<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="9-回文数1https:&#x2F;&#x2F;leetcode-cn.com&#x2F;problems&#x2F;palindrome-number&#x2F;    描述给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。 回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。 例如，121 是回文，而 123 不是。 示例 1： 输入：x &#x3D; 121输出：true示例 2： 输入：x &#x3D; -1">
<meta property="og:type" content="article">
<meta property="og:title" content="9-回文数">
<meta property="og:url" content="http://tallgy.gitee.io/2022/04/03/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/9-%E5%9B%9E%E6%96%87%E6%95%B0/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="9-回文数1https:&#x2F;&#x2F;leetcode-cn.com&#x2F;problems&#x2F;palindrome-number&#x2F;    描述给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。 回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。 例如，121 是回文，而 123 不是。 示例 1： 输入：x &#x3D; 121输出：true示例 2： 输入：x &#x3D; -1">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-04-03T07:47:42.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.338Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2022/04/03/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/9-%E5%9B%9E%E6%96%87%E6%95%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/04/03/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/9-%E5%9B%9E%E6%96%87%E6%95%B0/","path":"2022/04/03/算法/简单/9-回文数/","title":"9-回文数"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>9-回文数 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#9-%E5%9B%9E%E6%96%87%E6%95%B0"><span class="nav-number">1.</span> <span class="nav-text">9-回文数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8F%8F%E8%BF%B0"><span class="nav-number">1.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF"><span class="nav-number">1.2.</span> <span class="nav-text">思路</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%EF%BC%8C%E5%8F%8D%E8%BD%AC%EF%BC%8C%E5%9C%A8%E8%BD%AC%E6%95%B0%E5%AD%97"><span class="nav-number">1.2.1.</span> <span class="nav-text">转字符串，反转，在转数字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%96%E4%B8%BA%E5%AD%97%E7%AC%A6%E4%B8%B2%EF%BC%8C%E7%84%B6%E5%90%8Efor-%E5%BE%AA%E7%8E%AF%E4%BE%9D%E6%AC%A1%E8%BF%9B%E8%A1%8C%E6%AF%94%E8%BE%83"><span class="nav-number">1.2.2.</span> <span class="nav-text">化为字符串，然后for 循环依次进行比较</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%98%E6%96%B9%E9%A2%98%E8%A7%A3%EF%BC%8C%E5%8F%8D%E8%BD%AC%E4%B8%80%E5%8D%8A%E7%9A%84%E5%9B%9E%E6%96%87%E6%95%B0"><span class="nav-number">1.2.3.</span> <span class="nav-text">官方题解，反转一半的回文数</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/04/03/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/9-%E5%9B%9E%E6%96%87%E6%95%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          9-回文数
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-03 15:47:42" itemprop="dateCreated datePublished" datetime="2022-04-03T15:47:42+08:00">2022-04-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/" itemprop="url" rel="index"><span itemprop="name">简单</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="9-回文数"><a href="#9-回文数" class="headerlink" title="9-回文数"></a>9-回文数</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://leetcode-cn.com/problems/palindrome-number/</span><br></pre></td></tr></table></figure>



<h2 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h2><p>给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。</p>
<p>回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。</p>
<p>例如，121 是回文，而 123 不是。</p>
<p>示例 1：</p>
<p>输入：x = 121<br>输出：true<br>示例 2：</p>
<p>输入：x = -121<br>输出：false<br>解释：从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。<br>示例 3：</p>
<p>输入：x = 10<br>输出：false<br>解释：从右向左读, 为 01 。因此它不是一个回文数。</p>
<h2 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h2><ul>
<li>首先，负数直接不是回文数。</li>
<li>其次，回文数，反过来的数是和原数相等。</li>
</ul>
<h3 id="转字符串，反转，在转数字"><a href="#转字符串，反转，在转数字" class="headerlink" title="转字符串，反转，在转数字"></a>转字符串，反转，在转数字</h3><p>​        知道了特点之后，我们可以先将数转字符串，反转为数字，然后和原数比较是否相等。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">仅仅只需要短短几行代码就可以</span><br><span class="line">var isPalindrome = function (x) &#123;</span><br><span class="line">	// 首先，小于零的，直接false，由题可知的。</span><br><span class="line">	// 这样可以减少后面的计算。</span><br><span class="line">  if (x &lt; 0) return false</span><br><span class="line"></span><br><span class="line">	// 然后，我们使用隐式转换为字符串</span><br><span class="line">	// 利用字符串的 split 进行分割为数组，</span><br><span class="line">	// 然后 reverse 反转数组</span><br><span class="line">	// 最后在 join 合并为字符串。</span><br><span class="line">  const reverse = (x+&#x27;&#x27;).split(&#x27;&#x27;).reverse().join(&#x27;&#x27;)</span><br><span class="line">  </span><br><span class="line">  // 最后使用 弱比较，== 内部会进行隐式转换，不需要我们在转换为数字。</span><br><span class="line">  return x == reverse;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="化为字符串，然后for-循环依次进行比较"><a href="#化为字符串，然后for-循环依次进行比较" class="headerlink" title="化为字符串，然后for 循环依次进行比较"></a>化为字符串，然后for 循环依次进行比较</h3><p>​        这个我就简单的写核心部分就行了。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 就是简单的 循环进行比较。</span><br><span class="line">for (let i = 0; i &lt; str.length / 2; i++) &#123;</span><br><span class="line">  if (str[i] !== str[str.length - i - 1]) &#123;</span><br><span class="line">    return false</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="官方题解，反转一半的回文数"><a href="#官方题解，反转一半的回文数" class="headerlink" title="官方题解，反转一半的回文数"></a>官方题解，反转一半的回文数</h3><p>​        思路也不复杂，就是如果是回文数，那么反转一半，也应该是相等的。所以我们可以反转一半来进行判断。</p>
<p>​        我们最主要的问题就是，如果回文到了一半。这里直接说答案，就是如果我们回文数已经比原数大了，那么，说明回文到一半了。</p>
<p>​        这里我就不展示代码了。这个可以查看顶部原题链接查看官网的分析。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/04/01/JavaScript/Promise/JavaScript-Promise-all%E6%BA%90%E7%A0%81/" rel="prev" title="JavaScript-Promise.all源码">
                  <i class="fa fa-chevron-left"></i> JavaScript-Promise.all源码
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/04/04/Vue/vue-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%87%E4%BB%A4-loading%E7%9A%84%E4%BD%BF%E7%94%A8/" rel="next" title="vue-自定义指令-loading的使用">
                  vue-自定义指令-loading的使用 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
